/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <QSpacerItem>
#include <QTextBrowser>
#include "AboutThisApplication.h"

AboutThisApplication::AboutThisApplication(QWidget *parent)
    : QDialog(parent)
{
	ui.setupUi(this);

	QObject::connect(ui.closeButton, SIGNAL(clicked()), this, SLOT(accept()));
	QObject::connect(ui.creditsButton, SIGNAL(clicked()), this, SLOT(openCreditsDialog()));

	setCreditsDialog();
}

AboutThisApplication::~AboutThisApplication()
{

}

void AboutThisApplication::setCreditsDialog()
{
	mCreditsDialog.setWindowTitle("Credits");
	QTextBrowser* writtenByWidget = new QTextBrowser(&mTabWidget);
	QTextBrowser* documentedByWidget = new QTextBrowser(&mTabWidget);
	mTabWidget.addTab(writtenByWidget, tr("Written by"));
	mTabWidget.addTab(documentedByWidget, tr("Documented by"));

	writtenByWidget->setOpenExternalLinks(true);
	writtenByWidget->setOpenLinks(false);
	QString writtenByStr = "Yonglei Zheng &lt;<a href=\"zhengyonglei@gmail.com\">zhengyonglei@gmail.com</a>&gt;";
	writtenByWidget->insertHtml(writtenByStr);

	documentedByWidget->setOpenExternalLinks(true);
	documentedByWidget->setOpenLinks(false);
	QString documentedByStr = "Yonglei Zheng &lt;<a href=\"zhengyonglei@gmail.com\">zhengyonglei@gmail.com</a>&gt;";
	documentedByWidget->insertHtml(documentedByStr);

	mCloseCreditsDialogButton.setText(tr("Close"));
	QObject::connect(&mCloseCreditsDialogButton, SIGNAL(clicked()), &mCreditsDialog, SLOT(accept()));

	QGridLayout* grid = new QGridLayout(&mCreditsDialog);
	QSpacerItem* hSpacer = new QSpacerItem(20, 2, QSizePolicy::Expanding, QSizePolicy::Minimum);
	grid->addWidget(&mTabWidget, 0, 0, 1, 2);
	grid->addWidget(&mCloseCreditsDialogButton, 1, 1, 1, 1);
	grid->addItem(hSpacer, 1, 0, 1, 1);
}

void AboutThisApplication::openCreditsDialog()
{
	int x = this->geometry().x();
	int y = this->geometry().y();
	int width = this->geometry().width();
	int height = this->geometry().height();

	float sizeRate = 0.8f;
	mCreditsDialog.setGeometry(
			x+(1-sizeRate)*width/2,
			y+(1-sizeRate)*height/2,
			width*sizeRate,
			height*sizeRate);
	mCreditsDialog.exec();
}
